/**********************************************************************
*Created/Source by	:sfibich
*ScriptName			:metadata.proc.logValueInsert.sql
*Version			:1.3.120121
*Purpose			:creates a procedure for logging to the metadata logging table.
**********************************************************************/
SET NOCOUNT ON;

if exists (select * from sys.procedures where name='logValueInsert' and schema_name(schema_id)='metadata')
BEGIN
	drop procedure metadata.logValueInsert;
	print 'SUCCESS! drop procedure metadata.logValueInsert';
END
go
create procedure metadata.logValueInsert
@set_id uniqueidentifier 
,@name varchar(100)
,@value nvarchar(256)
,@raise_error bit=0
as
BEGIN
BEGIN TRY

INSERT INTO [utility].[metadata].[loggingValues]
([set_id]
,[name]
,[value])
VALUES
(@set_id
,@name
,@value);


END TRY
BEGIN CATCH
	if @raise_error=1
		BEGIN
			declare @error_message nvarchar(2048);
			set @error_message=ERROR_MESSAGE();
			RAISERROR(@error_message,10,1);	
		END
	else
	return(0);
END CATCH
END
go


if exists (select 1 from sys.objects where name='logValueInsert' and SCHEMA_NAME(schema_id)='metadata')
BEGIN
	merge metadata.utilityInfo as t
	using (
	select 
	'metadata.logValueInsert' as info_desc
	,'1.3.120121' as value
	) as s
	on
	(s.info_desc=t.info_desc)
	when matched then
	update 
	set 
	t.value=s.value
	,t.date_time=getdate()
	when not matched then 
	insert
	(info_desc,value)
	values
	(s.info_desc,s.value);
	print 'SUCCESS! create procedure metadata.logValueInsert';
END
ELSE
	print 'FAILURE! drop/create procedure metadata.logValueInsert';

